package ar.com.coonocer.joy.eclipseplugin.pages;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PropertyPage;

public class PluginPropertyPage extends PropertyPage {

	private static final int TEXT_FIELD_WIDTH = 50;

	public static final String PATH_OUTPUT_PROPERTY = "outputFolderPath";
	public static final String PATH_TEMPLATES_PROPERTY = "templatesFolderPath";
	public static final String TEMPLATE_GROUP_PROPERTY = "templateGroup";
	public static final String TEMPLATE_NAME_PROPERTY = "templateXmi2Mvn";

	private static final String PATH_OUTPUT_DEFAULT = "<Specify output folder path>";
	private static final String PATH_TEMPLATES_DEFAULT = "<Specify templates folder path>";
	private static final String TEMPLATE_GROUP_DEFAULT = "Xmi2Mvn";
	private static final String TEMPLATE_NAME_DEFAULT = "Default";

	private Text pathOutputText;
	private Text pathTemplatesText;
	private Text templateGroupText;
	private Text templateText;

	public PluginPropertyPage() {
		super();
	}

	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		GridData data = new GridData(GridData.FILL);
		data.grabExcessHorizontalSpace = true;
		composite.setLayoutData(data);
		//
		addSection(composite);
		return composite;
	}

	private Composite createDefaultComposite(Composite parent) {
		Composite composite = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);

		GridData data = new GridData();
		data.verticalAlignment = GridData.FILL;
		data.horizontalAlignment = GridData.FILL;
		composite.setLayoutData(data);

		return composite;
	}

	private void addSection(Composite parent) {
		Composite composite = createDefaultComposite(parent);
		// Path Templates
		//TODO hacer input file
		Label pathTemplatesLabel = new Label(composite, SWT.NONE);
		pathTemplatesLabel.setText("Templates Path:");
		//
		pathTemplatesText = new Text(composite, SWT.SINGLE | SWT.BORDER);
		GridData gd = new GridData();
		gd.widthHint = convertWidthInCharsToPixels(TEXT_FIELD_WIDTH);
		pathTemplatesText.setLayoutData(gd);
		pathTemplatesText.setText(getProperty(PATH_TEMPLATES_PROPERTY, PATH_TEMPLATES_DEFAULT));
		//
		// Output Path
		Label pathLabel = new Label(composite, SWT.NONE);
		pathLabel.setText("Output Path:");
		//
		//TODO hacer input file
		pathOutputText = new Text(composite, SWT.SINGLE | SWT.BORDER);
		gd = new GridData();
		gd.widthHint = convertWidthInCharsToPixels(TEXT_FIELD_WIDTH);
		pathOutputText.setLayoutData(gd);
		pathOutputText.setText(getProperty(PATH_OUTPUT_PROPERTY, PATH_OUTPUT_DEFAULT));
		//
		// Separator
		Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		separator.setLayoutData(gridData);
		//
		// Template group
		Label tamplateGroupLabel = new Label(composite, SWT.NONE);
		tamplateGroupLabel.setText("Template Group:");
		//
		//TODO hacer combo
		templateGroupText = new Text(composite, SWT.SINGLE | SWT.BORDER);
		gd = new GridData();
		gd.widthHint = convertWidthInCharsToPixels(TEXT_FIELD_WIDTH);
		templateGroupText.setLayoutData(gd);
		templateGroupText.setText(getProperty(TEMPLATE_GROUP_PROPERTY, TEMPLATE_GROUP_DEFAULT));
		//
		// Template
		Label tamplateLabel = new Label(composite, SWT.NONE);
		tamplateLabel.setText("Template Name:");
		//
		//TODO hacer combo
		templateText = new Text(composite, SWT.SINGLE | SWT.BORDER);
		gd = new GridData();
		gd.widthHint = convertWidthInCharsToPixels(TEXT_FIELD_WIDTH);
		templateText.setLayoutData(gd);
		templateText.setText(getProperty(TEMPLATE_NAME_PROPERTY, TEMPLATE_NAME_DEFAULT));
	}

	protected void performDefaults() {
		pathOutputText.setText(PATH_OUTPUT_DEFAULT);
		pathTemplatesText.setText(PATH_TEMPLATES_DEFAULT);
		templateGroupText.setText(TEMPLATE_GROUP_DEFAULT);
		templateText.setText(TEMPLATE_NAME_DEFAULT);
	}

	public boolean performOk() {
		try {
			setPropertyWithText(PATH_OUTPUT_PROPERTY, pathOutputText.getText());
			setPropertyWithText(PATH_TEMPLATES_PROPERTY, pathTemplatesText.getText());
			setPropertyWithText(TEMPLATE_GROUP_PROPERTY, templateGroupText.getText());
			setPropertyWithText(TEMPLATE_NAME_PROPERTY, templateText.getText());
		} catch (CoreException e) {
			return false;
		}
		return true;
	}

	private String getProperty(String propertyName, String defaultValue) {
		try {
			String value = ((IResource) getElement()).getPersistentProperty(new QualifiedName("", propertyName));
			return (value != null) ? value : defaultValue;
		} catch (CoreException e) {
			return defaultValue;
		}
	}

	private void setPropertyWithText(String propertyName, String value) throws CoreException {
		((IResource) getElement()).setPersistentProperty(new QualifiedName("", propertyName), value);
	}

}